//
//  ZJDistrictSeleView.m
//  GongXinDangJian
//
//  Created by ZJ on 2017/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZJDistrictSeleView.h"

#define ConfirmButtonTitleColor         [UIColor colorWithRed:0.26f green:0.56f blue:1.00f alpha:1.00f]

@interface ZJDistrictSeleView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSArray *_addressArray;/**< 所有的数据 */
    NSArray *_townArray;/**< 市的数组 */
    NSArray *_cityArray;/**< 城市的数组 */
}
/** 省 */
@property (nonatomic, assign)NSString *province;
/** 市 */
@property (nonatomic, assign)NSString *town;
/** 城市 */
@property (nonatomic, assign)NSString *city;
/** 4.上午、下午、晚上   */
@property (strong, nonatomic) NSString   *string;

@property(nonatomic,strong)UIPickerView  * pickerView;
@property(nonatomic,strong)UIView * baseView;

@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * cancelBtn;
@property(nonatomic,strong)UIButton * sureBtn;

@property(nonatomic,strong)NSString * dateString;
/**  日期数组 这是除了年月日之外重新加的数据，列如：上午，下午，晚上 */
@property (strong, nonatomic) NSArray  *dateArray;

@end

@implementation ZJDistrictSeleView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSString *cityString = [[NSBundle mainBundle] pathForResource:@"省市县" ofType:@"json"];
        NSData *cityData = [NSData dataWithContentsOfFile:cityString];
        _addressArray = [[NSJSONSerialization JSONObjectWithData:cityData options:kNilOptions error:nil] objectForKey:@"province"];
        _townArray = [_addressArray.firstObject objectForKey:@"city"];
        _cityArray = [_townArray.firstObject objectForKey:@"district"];
        
        self.province = [_addressArray.firstObject objectForKey:@"name"];
        self.town = [_townArray.firstObject objectForKey:@"name"];
        self.city = [_cityArray.firstObject objectForKey:@"name"];
    }
    return self;
}

-(void)setUI{
    
    [self addTitleAndBtn];
    
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.pickerView];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
}

#pragma mark  pickerView的代理方法

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return _addressArray.count;
    }else if(component == 1) {
        return _townArray.count;
    }else  {
        return _cityArray.count;
    }
}

/**  组的个数  */
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        _townArray = [_addressArray[row] objectForKey:@"city"];
        _cityArray = [_townArray.firstObject objectForKey:@"district"];
        [pickerView reloadAllComponents];
    }else if(component == 1) {
        _cityArray = [_townArray[row] objectForKey:@"district"];
        [pickerView reloadAllComponents];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        self.province = [_addressArray[row] objectForKey:@"name"];
        return self.province;
    }else  if (component == 1) {
        self.town = [_townArray[row] objectForKey:@"name"];
        return self.town ;
    }else {
        self.city = [_cityArray[row] objectForKey:@"name"];
        return self.city;
    }
    
}
/**  单元格的高度  */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 40;
}


/**  添加头部  */
-(void)addTitleAndBtn{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
    view.backgroundColor = [UIColor viewBGGrayColor];
    [self.baseView addSubview:view];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, 20)];
    label.text = @"选择时间";
    label.textAlignment = NSTextAlignmentCenter;
    [self.baseView addSubview:label];
    NSArray * btnTitle = @[@"取消",@"确定"];
    for (int i = 0; i < 2; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:btnTitle[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake((i == 0)?0:(self.bounds.size.width - 60), 0, 60, 40);
        if (i == 0) {
            _cancelBtn = btn;
        }else
            _sureBtn = btn;
        [btn setTitleColor:ConfirmButtonTitleColor forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.baseView addSubview:btn];
    }
    
    
}
/** 点击事件  */
-(void)btnClick:(UIButton *)btn{
    _dateString = [NSString stringWithFormat:@"%@%@%@",self.province,self.town,self.city];
    if (btn == _cancelBtn) {
        [self hiden];
    }else{
        if (self.delegate) {
            [self.delegate  ZJDistrictSeleViewDelegate_String:_dateString];
        }
        [self hiden];
    }
    btn.selected = !btn.selected;
}

-(UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width, self.baseView.frame.size.height - 40)];
        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
}
- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - 240, [UIScreen zj_screenWith], 240 * [UIScreen zj_screenHScale])];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(void)hiden{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.baseView.frame;
        
        rect.origin.y += self.baseView.bounds.size.height;
        
        self.baseView.frame = rect;
        
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setUI];
    
    self.baseView.frame = CGRectMake(0, [UIScreen zj_screenHeight], [UIScreen zj_screenWith],  240 * [UIScreen zj_screenHScale]);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect rect = self.baseView.frame;
        
        rect.origin.y = [UIScreen zj_screenHeight] - self.baseView.bounds.size.height;
        self.baseView.frame = rect;
        self.backgroundColor = [UIColor zj_colorWithR:10 G:10 B:10 Al:0.2];
        
        
    }];
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self hiden];
}

@end
